//#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "myOpGLViewFrame.h"
//#include "myOpGLViewDlg.h"
#include "myOpGLViewApp.h"
#include "myOpGLViewCanvas.h"
#include "wx/sizer.h"
#include <wx/timer.h>

#define ID_STATUSBAR      10102

#define ID_MNU_DRAW      10200
#define ID_MNU_QUIT      10900
#define ID_MNU_ABOUT     10800
#define ID_TIMER         20100

BEGIN_EVENT_TABLE(myOpGLViewFrame, wxFrame)
    EVT_MENU(ID_MNU_QUIT,  myOpGLViewFrame::OnQuitClick)
    EVT_MENU(ID_MNU_ABOUT, myOpGLViewFrame::OnAboutClick)
    EVT_MENU(ID_MNU_DRAW, myOpGLViewFrame::OnDrawClick)
    EVT_SIZE(myOpGLViewFrame::resized)
    EVT_TIMER(ID_TIMER, myOpGLViewFrame::DrawTimer)
END_EVENT_TABLE()

myOpGLViewFrame::myOpGLViewFrame(wxWindow* parent, wxWindowID id, const wxString& caption,
                                 const wxPoint& pos, const wxSize& size, long style) : myTimer(this, ID_TIMER)
{
 Create(parent,id,caption,pos,size,style);
}

myOpGLViewFrame::~myOpGLViewFrame()
{}

bool myOpGLViewFrame::Create(wxWindow* parent, wxWindowID id, const wxString& caption,
                        const wxPoint& pos, const wxSize& size, long style)
{
		wxFrame::Create(parent,id,caption,pos,size,style);
        SetIcon(wxICON(monicone));
        //STATUS BAR
        wxStatusBar* StatusBar = new wxStatusBar(this, ID_STATUSBAR, wxST_SIZEGRIP|wxNO_BORDER);
        StatusBar->SetFieldsCount(1);
        SetStatusBar(StatusBar);
        SetStatusText(_T("Welcome to wxOpenGLView v0.0 !"));
        //MENU BAR
        wxMenuBar* menuBar = new wxMenuBar;
        
        wxMenu* menuFile = new wxMenu;
        menuFile->Append(ID_MNU_DRAW, _T("&Draw\tAlt-D"), _T("Draw an example of wxCanvas"));
        menuFile->Append(ID_MNU_QUIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
        
        wxMenu* helpMenu = new wxMenu;
        helpMenu->Append(ID_MNU_ABOUT, _T("&About\tF1"), _T("Show about dialog"));

        menuBar->Append(menuFile, _T("&File"));
        menuBar->Append(helpMenu, _T("&Help"));
        SetMenuBar(menuBar);
        //CONTENT OF THE MAIN FRAME
        wxBoxSizer* principal_sizer = new wxBoxSizer(wxVERTICAL);
        myGLCanvas = new myOpGLViewCanvas(this, -1, wxPoint(0,0), wxSize(480,480),
                                   wxNO_BORDER, _("test"));
        principal_sizer->Add(myGLCanvas, 1, wxGROW, 1);
        SetSizer(principal_sizer);
        myGLCanvas->undraw();
        myGLCanvas->angle = 0;
		Centre(wxHORIZONTAL);
        return true;
}

void myOpGLViewFrame::OnQuitClick(wxCommandEvent& event)
{
     Close(true);
     event.Skip();
}

void myOpGLViewFrame::OnAboutClick(wxCommandEvent& event)
{
    wxString msg;
    msg.Printf( _T("This is a simple OpenGL viewer.\n")
                _T("Developed by MaX\n")
                _T("Copyright (C) 2009"));

    wxMessageBox(msg, _T("About wxOpenGLView"), wxOK | wxICON_INFORMATION, this);
    event.Skip();
}

void myOpGLViewFrame::OnDrawClick(wxCommandEvent& event)
{

 	 if(myTimer.IsRunning()){
            myTimer.Stop();
            myGLCanvas->undraw();
     }
 	 else
			myTimer.Start(20);
     //event.Skip();
}

void myOpGLViewFrame::resized(wxSizeEvent& evt)
{
  OnSize(evt);
  Refresh(FALSE);
}

void myOpGLViewFrame::DrawTimer(wxTimerEvent& event)
{
     if(myTimer.IsRunning())
          myGLCanvas->draw();
}
